<template>
  <a-modal
    :title="`${ okHidden ? '' : model && model.id ? '编辑' : '新增'}服务分类`"
    :ok-button-props="{ style: okHidden && { display: 'none' } }"
    :width="680"
    :visible="visible"
    :confirmLoading="loading"
    :maskClosable="false"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item v-if="model && model.id" label="主键ID" hidden>
          <a-input v-decorator="['id', { initialValue: model.id }]" disabled />
        </a-form-item>
        <a-form-item label="权限名称">
          <a-input :disabled="okHidden" placeholder="请输入权限名称" v-decorator="['name']" />
        </a-form-item>
        <a-form-item label="权限别名">
          <a-input :disabled="okHidden" placeholder="请输入权限别名" v-decorator="['display_name']" />
        </a-form-item>
        <a-form-item label="父级权限">
          <a-input :disabled="okHidden" placeholder="请输入父级权限" v-decorator="['pid']" />
        </a-form-item>
        <a-form-item label="几级权限">
          <a-input :disabled="okHidden" placeholder="请输入几级权限" v-decorator="['level']" />
        </a-form-item>
        <!-- <a-form-item label="服务分类">
          <a-select :disabled="okHidden" placeholder="请选择服务分类" v-decorator="['service_id', {rules: [{required: true, message: '请选择至少一个服务分类！'}]}]" >
            <a-select-option :value="item.id" v-for="item in serveList" :key="item.id">
              {{ item.service_name }}
            </a-select-option>
          </a-select>
        </a-form-item> -->
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 表单字段
const fields = ['service_id', 'title', 'image', 'content', 'publisher']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    okHidden: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    serveList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 4 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 }
      }
    }
    return {
      form: this.$form.createForm(this),
      currentLength: 0,
      ruleContent: ''
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    btnSetTxt (content) {
      this.ruleContent = content
    }
  }
}
</script>
